<template>
  <div>
    <!-- 面包屑 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>消息</el-breadcrumb-item>
      <el-breadcrumb-item>消息列表</el-breadcrumb-item>
    </el-breadcrumb>

    <el-card shadow="always" :body-style="{ padding: '20px' }">
      <!-- 搜索 -->
      <el-input
        placeholder="请输入内容"
        class="input-with-select"
        v-model="queryInfo.query"
        clearable
        style="width: 30%; margin-bottom: 15px"
        @clear="getTaskDynamics"
        @keyup.enter.native="handleSearch"
      >
        <el-button
          slot="append"
          type="primary"
          icon="el-icon-search"
          @click="getTaskDynamics"
        ></el-button>
      </el-input>

      <!-- 展示表格 -->
      <el-table :data="taskDynamics">
        <el-table-column prop="" label="操作时间" width="180px">
          <template slot-scope="scope">
            <div>
              {{ scope.row.createdAt | date }}
            </div>
          </template>
        </el-table-column>

        <el-table-column prop="" label="任务名称">
          <template slot-scope="scope">
            <span v-if="scope.row.taskId">
              {{ scope.row.taskId.taskname }}
            </span>
          </template>
        </el-table-column>

        <!-- <el-table-column label="操作类型" width="100px">
          <template slot-scope="scope">
            <div v-if="scope.row.operate === 0">提交任务</div>
            <div v-if="scope.row.operate === 1">更新任务</div>
            <div v-if="scope.row.operate === 2">添加任务</div>
            <div v-if="scope.row.operate === 3">删除任务</div>
            <div v-if="scope.row.operate === 4">任务审核通过</div>
            <div v-if="scope.row.operate === 5">打回任务</div>
            <div v-if="scope.row.operate === 6">提醒</div>
          </template>
        </el-table-column> -->

        <el-table-column prop="" label="操作人" width="100px">
          <template slot-scope="scope">
            <span v-if="scope.row.userId.name">
              <span>
                {{ scope.row.userId.name }}
              </span>
            </span>
            <span v-else> 管理员 </span>
          </template>
        </el-table-column>

        <el-table-column prop="" label="通知人">
          <template slot-scope="scope">
            <span v-for="(item, index) in scope.row.notifier" :key="index">
              {{ item.name }}
            </span>
          </template>
        </el-table-column>

        <el-table-column prop="" label="消息描述" width="450px">
          <template slot-scope="scope" class="aa">
            <span v-html="scope.row.tips"></span>
          </template>
        </el-table-column>

        <el-table-column label="操作" width="200px">
          <template slot-scope="scope">
            <span v-if="scope.row.taskId" style="margin-right:10px">
              <el-button
                type="success"
                size="mini"
                @click="$router.push(`/auditConfirm/${scope.row.taskId._id}`)"
                >查看</el-button
              >
            </span>

            <span v-else style="margin-right:10px">
              <el-button
                type="success"
                size="mini"
                disabled
                >查看</el-button
              >
            </span>

            <el-button type="danger" size="mini" @click="noticeDel(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <el-pagination
        background
        layout="total, prev, pager, next"
        :total="total"
        style="margin-top: 20px"
        :current-page="queryInfo.pagenum"
        :page-size="queryInfo.pagesize"
        @current-change="handleCurrentChange"
      >
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
import dayjs from "dayjs";
export default {
  filters: {
    date(val) {
      return dayjs(val).format("YYYY-MM-DD HH:mm:ss");
    },
    describe(val) {
      var data = val.split("<h3>")[1].split("</h3>")[0];
      return data;
    },
  },
  data() {
    return {
      taskDynamics: [],
      queryInfo: {
        status: this.$route.query.status * 1 || 0,
        pagenum: this.$route.query.pagenum * 1 || 1, //第几页
        pagesize: 7, //每页显示个数
      },
      //总条数
      total: 0,
    };
  },
  methods: {
    //获取消息列表
    async getTaskDynamics() {
      let res = await this.$http.get(`/journal/taskDynamics`, {
        params: this.queryInfo,
      });
      let data = res.data;
      if (data.status === 200) {
        this.taskDynamics = data.data.data;
        this.queryInfo.pagenum = data.data.pagenum;
        this.total = data.data.total;
      }
    },

    //页数改变触发事件
    handleCurrentChange(val) {
      this.queryInfo.pagenum = val;
      this.$router.replace({
        query: {
          status: this.$route.query.status,
          pagenum: val,
        },
      });
      this.getTaskDynamics();
    },

    // 删除消息
    async noticeDel(val) {
      this.$confirm(`是否确定删除该消息`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          let res = await this.$http.get(`/journal/delNotice/${val._id}`);
          this.$message({
            type: "success",
            message: "删除成功!",
          });
          this.getTaskDynamics();
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },

    // 搜索Enter键盘事件
    handleSearch(val) {
      if (val.key == "Enter") {
        this.getTaskDynamics();
      }
    },
  },
  created() {
    this.getTaskDynamics();
  },
};
</script>

<style lang="less" scoped>
// *{
//     font-weight: bold;
// }
.status {
  margin-bottom: 15px;
}
/deep/ h3 {
  display: inline-block;
}
</style>